package util.content;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class ContentRex {

	List sourceSiteList = null;

	public String getTitle() {
		for (Iterator iterSourceSite = sourceSiteList.iterator(); iterSourceSite.hasNext();) {
			Element sourceSite = (Element) iterSourceSite.next();
			String titleRex = sourceSite.getChildText("titleRex");
			Pattern p = Pattern.compile(titleRex);
			Matcher m = p.matcher(title);
			if (m.find()) {
				title = m.group(1).trim();
			}
		}
		return title;
	}

	String contentString = "";

	public String getContentString() {
		final String NoSelectedAnswer = "9527";
		contentString = "";
		String partialContent = null;
		ContentCapture cc = new ContentCapture();
		RandomContent rc = new RandomContent();
		for (Iterator iterSourceSite = sourceSiteList.iterator(); iterSourceSite.hasNext();) {
			Element sourceSite = (Element) iterSourceSite.next();
			// 拿取可以识别是否在回答中的问题的标记字符串，并在bodyString中检索发现了点话说明正在回答中不抓取但做一个标记
			String during = sourceSite.getChildText("during");
			Pattern p = Pattern.compile(during);
			Matcher m = p.matcher(bodyString);
			m.matches();
			m.reset();
			if (!m.find()) {
				List spiderList = sourceSite.getChildren("spider");
				for (Iterator iterSpider = spiderList.iterator(); iterSpider.hasNext();) {
					Element spider = (Element) iterSpider.next();

					// 抓取提问和补充提问
					List questionList = spider.getChildren("question");
					for (Iterator iterQuestion = questionList.iterator(); iterQuestion.hasNext();) {
						Element question = (Element) iterQuestion.next();
						// 拿到抓取内容的正则
						String questionPreRex = question.getChildText("questionPre");
						// System.out.println(contentRex);
						// 调用ContentCapture对象
						cc.setContentRex(questionPreRex);
						String questionSuply = question.getChildText("questionSuply");
						// 这里先设置了问题部分的追问也就是补充问题的正则表达式，这和回答部分的追问正则是不同的
						cc.setReAskRex(questionSuply);
						partialContent = cc.getContent(bodyString);
						// System.out.println(partialContent);
						// 拿到需要删除的内容的正则
						List questionDelList = question.getChildren("questionDel");
						for (Iterator iterQuestionDel = questionDelList.iterator(); iterQuestionDel.hasNext();) {
							Element questionDel = (Element) iterQuestionDel.next();
							String questionDelRex = questionDel.getText();
							if (questionDelRex == "9527") {
								System.out.println("del - 9527");
							} else {
								cc.setDelRex(questionDelRex);
								partialContent = cc.del(partialContent);
							}
						}
						contentString = contentString + partialContent;
					}

					// 拿到追问正则
					List reAskList = spider.getChildren("reask");
					for (Iterator iterReAsk = reAskList.iterator(); iterReAsk.hasNext();) {
						Element reAsk = (Element) iterReAsk.next();
						String reAskRex = reAsk.getText();
						cc.setReAskRex(reAskRex);
					}

					// 抓取最佳答案
					List contentBestList = spider.getChildren("conBest");
					for (Iterator iterContentBest = contentBestList.iterator(); iterContentBest.hasNext();) {
						Element contentBest = (Element) iterContentBest.next();
						// 先抓取最佳的部分块
						String contentRex[] = new String[2];
						contentRex[0] = contentBest.getChildText("contentget");
						contentRex[1] = contentBest.getChildText("contentTuijian");
						// 拿到抓取内容的正则,再把最佳答案部分拿出来
						String bestArea = contentBest.getChildText("bestArea");
						Pattern bestAreaPattern = Pattern.compile(bestArea);
						Matcher bestAreaMatcher = bestAreaPattern.matcher(bodyString);
						bestAreaMatcher.reset();
						// 如果最佳回答区域是存在的，也就是有最佳回答在执行抓取操作
						if (bestAreaMatcher.find()) {
							String bestAreaString = bestAreaMatcher.group();
							for (int i = 0; i < contentRex.length; i++) {
								// System.out.println(contentRex);
								// 调用ContentCapture对象
								cc.setContentRex(contentRex[i]);
								// 这里bodyString做一个处理，把bodyString分成最佳答案和普通答案部分

								partialContent = cc.getContent(bestAreaString);
								// System.out.println(partialContent);
								// 拿到需要删除的内容的正则
								List contentdelList = contentBest.getChildren("contentdel");
								for (Iterator iterContentdel = contentdelList.iterator(); iterContentdel.hasNext();) {
									Element contentdel = (Element) iterContentdel.next();
									String delRex = contentdel.getText();
									if (delRex == "9527") {
										System.out.println("del - 9527");
									} else {
										cc.setDelRex(delRex);
										partialContent = cc.del(partialContent);
									}
								}
								// 调用了乱序类
								rc.contentAdd(partialContent);
							}
							contentString = contentString + rc.getWycContent();
							rc.clear();
						}
					}
					// 抓取普通答案
					List contentList = spider.getChildren("content");
					for (Iterator iterContent = contentList.iterator(); iterContent.hasNext();) {
						Element content = (Element) iterContent.next();
						// 拿到抓取内容的正则
						String contentRex = content.getChildText("contentget");
						// 拿到抓取内容的正则,再把最佳答案部分拿出来
						String generalArea = content.getChildText("generalArea");
						Pattern generalAreaPattern = Pattern.compile(generalArea);
						Matcher generalAreaMatcher = generalAreaPattern.matcher(bodyString);
						generalAreaMatcher.reset();
						// 如果普通回答区域是存在的，也就是有普通回答在执行抓取操作
						if (generalAreaMatcher.find()) {
							String generalAreaString = generalAreaMatcher.group();
							// 调用ContentCapture对象
							cc.setContentRex(contentRex);
							partialContent = cc.getContent(generalAreaString);
							List contentdelList = content.getChildren("contentdel");
							for (Iterator iterContentdel = contentdelList.iterator(); iterContentdel.hasNext();) {
								Element contentdel = (Element) iterContentdel.next();
								// 拿到需要删除的内容的正则
								String delRex = contentdel.getText();
								if (delRex == "9527") {
									System.out.println("del - 9527");
								} else {
									cc.setDelRex(delRex);
									partialContent = cc.del(partialContent);
								}
							}
							// 这里拼了一个格式
							contentString = contentString + partialContent;
						}
					}
				}
			}
			// 正在回答中的字符串返回9527作为标记
			else {

				contentString = "9527";
			}
		}
		return contentString;
	}

	ContentRex() {

		try {
			String xmlpath = "spiderParameter.xml";
			SAXBuilder builder = new SAXBuilder(false);
			Document doc = builder.build(xmlpath);
			Element sourceSite = doc.getRootElement();
			sourceSiteList = sourceSite.getChildren("sourceSite");
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	String bodyString = null;

	public void setBodyString(String bodyString) {
		this.bodyString = bodyString;
	}

	String title = null;

	public void setTitle(String title) {
		this.title = title;
	}
}
